package com.example.housingRentalSystem.service.Impl;

import com.example.housingRentalSystem.bean.Feedback;
import com.example.housingRentalSystem.mapper.FeedbackMapper;
import com.example.housingRentalSystem.service.FeedbackService;
import com.example.housingRentalSystem.utils.PageRequest;
import com.example.housingRentalSystem.utils.PageResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @author 王硕
 * @version jdk21
 * @className FeedbackServiceImpl
 * @description
 * @date 2025/3/24 10:42
 */
@Service
public class FeedbackServiceImpl implements FeedbackService {

    private static final Logger logger = LoggerFactory.getLogger(FeedbackServiceImpl.class);

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Override
    @Transactional
    public void createFeedback(Feedback feedback, Integer userId) {

        if (feedback.getTitle() == null || feedback.getTitle().trim().isEmpty()) {
            throw new IllegalArgumentException("反馈标题不能为空");
        }

        feedback.setCreateTime(new Date());
        feedback.setStatus(0);
        feedback.setIsDelete(0);
        feedback.setUserId(userId);

        int result = feedbackMapper.insert(feedback);
        if (result != 1) {
            logger.error("反馈创建失败，用户ID：{}", userId);
            throw new RuntimeException("反馈提交失败");
        }
    }

    @Override
    @Transactional
    public void replyFeedback(Integer feedbackId, String reply) {

        Feedback feedback = feedbackMapper.selectById(feedbackId);
        if (feedback == null || feedback.getIsDelete() == 1) {
            logger.warn("无效反馈ID: {}", feedbackId);
            throw new RuntimeException("反馈不存在或已删除");
        }

        Feedback updateEntity = new Feedback();
        updateEntity.setId(feedbackId);
        updateEntity.setReply(reply);
        updateEntity.setStatus(1);
        updateEntity.setContactName(feedback.getContactName());
        updateEntity.setContactEmail(feedback.getContactEmail());

        int result = feedbackMapper.updateFeedback(updateEntity);
        if (result != 1) {
            logger.error("回复更新失败: feedbackId={}", feedbackId);
            throw new RuntimeException("系统繁忙，请稍后重试");
        }
    }


    @Override
    @Transactional
    public void deleteFeedback(Integer feedbackId, Integer userId) {
        Feedback feedback = feedbackMapper.selectById(feedbackId);
        if (feedback == null) {
            throw new IllegalArgumentException("反馈不存在");
        }
        if (!feedback.getUserId().equals(userId)) {
            logger.warn("非法删除尝试，用户ID：{}，反馈用户ID：{}", userId, feedback.getUserId());
            throw new SecurityException("无权删除他人反馈");
        }

        int result = feedbackMapper.deleteById(feedbackId);
        if (result != 1) {
            logger.error("反馈删除失败，ID：{}", feedbackId);
            throw new RuntimeException("删除操作失败");
        }
    }

    @Override
    public PageResponse<Feedback> getFeedbackList(
            Integer status,
            Integer userId,
            PageRequest pageRequest
    ) {
        PageHelper.startPage(pageRequest.getPage(), pageRequest.getSize());

        Page<Feedback> page = feedbackMapper.selectByConditions(status, userId);

        return PageResponse.build(page);
    }

    @Override
    public int selectCountFeedback() {
        int count = feedbackMapper.selectCountFeedback();
        return count;
    }
}